<script setup>
import {onMounted, ref} from "vue";
import { User, Lock, View, Hide} from '@element-plus/icons-vue'
import {userLoginService} from "@/api/user.js";
import {  RouterView,useRouter } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import useUserInfoStore from "@/stores/userInfo.js";
import menusInfoStore from "@/stores/menu.js";
const loginFormRef=ref({})
const router = useRouter()
const menuInfoStore = menusInfoStore();
const tokenStore = useTokenStore()
const userInfoStore=useUserInfoStore()
const loginForm=ref({
  username: '',
  password: ''
})
const rememberMe = ref(false); // 用于记录用户是否选择记住我
onMounted(() => {
  const savedUsername = localStorage.getItem('username');
  const savedPassword = localStorage.getItem('password');
  if (savedUsername && savedPassword) {
    loginForm.value.username = savedUsername;
    loginForm.value.password = savedPassword;
    rememberMe.value = true; // 自动勾选“记住我”
  }
});
const login = async () => {
  // 如果选择了记住我，将用户名和密码保存到 localStorage
  if (rememberMe.value) {
    localStorage.setItem('username', loginForm.value.username);
    localStorage.setItem('password', loginForm.value.password);
  } else {
    localStorage.removeItem('username');
    localStorage.removeItem('password');
  }

  let result = await userLoginService(loginForm.value);

  // 保存用户信息和token到Pinia store
  userInfoStore.setUserInfo(result.data);
  tokenStore.setToken(result.data.token);
  console.log("data数据" + JSON.stringify(result.data));
  console.log("token数据" + result.data.token);
  menuInfoStore.setMenusInfo(result.data.menus);
  console.log("menus数据" + JSON.stringify(menuInfoStore.menusInfo));
  // 登录成功后跳转到首页
  await router.push('/');
};
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ]
}
const showPassword = ref(false);
const togglePassword = () => {
  showPassword.value = !showPassword.value;
};
</script>

<template>
  <div class="background">
    <div class="form-login">
      <div class="form">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form
            ref="loginFormRef"
            status-icon
            :model="loginForm"
            :rules="rules"
        >
          <el-form-item  prop="username" class="form-item">
            <el-input :prefix-icon="User" v-model="loginForm.username"placeholder="请输入用户名"  type="text" style="width: 100%"></el-input>
          </el-form-item>
          <el-form-item prop="password" class="form-item">
            <el-input
                :prefix-icon="Lock"
                :suffix-icon="showPassword ?  Hide: View"
                v-model="loginForm.password"
                placeholder="请输入密码"
                :type="showPassword ? 'text' : 'password'"
                style="width: 100%"
                @click="togglePassword"
            ></el-input>
          </el-form-item>
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
          </div>
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space @click="login" style="">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.background {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #6e8efb, #a777e3);
}

.form-login {
  width: 400px;
  padding: 40px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.form {
  width: 100%;
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
}

.form-item {
  margin-bottom: 25px;
}

.button {
  width: 100%;
  height: 45px;
  margin-top: 10px;
}

.flex {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.el-input {
  height: 45px;
}

.el-input__inner {
  height: 100%;
}
</style>
